home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / win_q_t / trem.zip / CONNECT.C < prev    next >
Text File  |  1991-05-11  |  5KB  |  178 lines

  1. /************************************************************************
  2.  *
  3.  *    Copyright (c) 1991 Microsoft Corporation.  All Rights Reserved.
  4.  *
  5.  *-----------------------------------------------------------------------
  6.  *
  7.  *     Project:  Windows Terminal Example
  8.  *
  9.  *      Module:  connect.c
  10.  *
  11.  *      Author:  Bryan A. Woodruff (baw)
  12.  *
  13.  *
  14.  *     Remarks:  This handles the connection to the COM port.
  15.  *
  16.  *   Revisions:
  17.  *     01.00.000  5/10/91 baw   Wrote it.
  18.  *
  19.  ************************************************************************/
  20.  
  21. #include "terminal.h"
  22.  
  23. /************************************************************************
  24.  *  BOOL OpenConnection( HWND hWnd )
  25.  *
  26.  *  Description:
  27.  *     Opens communication port specified in the TERMINFO struct.
  28.  *     It also sets the CommState and notifies the window via
  29.  *     the fConnected flag in the TERMINFO struct.
  30.  *
  31.  *  Comments:
  32.  *      5/ 9/91  baw  Wrote this nifty comment block.
  33.  *
  34.  ************************************************************************/
  35.  
  36. BOOL OpenConnection( HWND hWnd )
  37. {
  38.    char         szPort[5] ;
  39.    BOOL         fRetVal ;
  40.    HCURSOR      hOldCursor, hWaitCursor ;
  41.    LOCALHANDLE  hTermInfo ;
  42.    NPTERMINFO   npTermInfo ;
  43.  
  44.    hTermInfo = GetWindowWord( hWnd, GWW_TERMINFO ) ;
  45.    if (NULL == (npTermInfo = (NPTERMINFO) LocalLock( hTermInfo )))
  46.       return ( FALSE ) ;
  47.  
  48.    // show the hourglass cursor
  49.    hWaitCursor = LoadCursor( NULL, IDC_WAIT ) ;
  50.    hOldCursor = SetCursor( hWaitCursor ) ;
  51.  
  52.    lstrcpy( szPort, COMPREFIX ) ;
  53.    szPort[3] = (char) ('0' + npTermInfo -> bPort) ;
  54.    szPort[4] = NULL ;
  55.    if ((npTermInfo -> nCid = OpenComm( szPort, RXQUEUE, TXQUEUE )) < 0)
  56.    {
  57.       LocalUnlock( hTermInfo ) ;
  58.       return ( FALSE ) ;
  59.    }
  60.  
  61.    fRetVal = SetupConnection( hWnd ) ;
  62.  
  63.    if (fRetVal)
  64.    {
  65.       npTermInfo -> fConnected = TRUE ;
  66.       SetEvent( hWnd, npTermInfo -> nCid, EV_RXCHAR, WM_EVENT_RXCHAR ) ;
  67.       SetTerminalFocus( hWnd ) ;
  68.    }
  69.    else
  70.    {
  71.       npTermInfo -> fConnected = FALSE ;
  72.       CloseComm( npTermInfo -> nCid ) ;
  73.    }
  74.  
  75.    // restore cursor
  76.    SetCursor( hOldCursor ) ;
  77.  
  78.    LocalUnlock( hTermInfo ) ;
  79.    return ( fRetVal ) ;
  80.  
  81. } /* end of OpenConnection() */
  82.  
  83. /************************************************************************
  84.  *  BOOL SetupConnection( HWND hWnd )
  85.  *
  86.  *  Description:
  87.  *     This routine sets the communication parameters.
  88.  *
  89.  *  Comments:
  90.  *      5/ 9/91  baw  Wrote it.
  91.  *
  92.  ************************************************************************/
  93.  
  94. BOOL SetupConnection( HWND hWnd )
  95. {
  96.    BOOL         fRetVal ;
  97.    BYTE         bSet ;
  98.    DCB          dcb ;
  99.    LOCALHANDLE  hTermInfo ;
  100.    NPTERMINFO   npTermInfo ;
  101.  
  102.    hTermInfo = GetWindowWord( hWnd, GWW_TERMINFO ) ;
  103.    if (NULL == (npTermInfo = (NPTERMINFO) LocalLock( hTermInfo )))
  104.       return ( FALSE ) ;
  105.  
  106.    GetCommState( npTermInfo -> nCid, &dcb ) ;
  107.  
  108.    dcb.BaudRate = npTermInfo -> wBaudRate ;
  109.    dcb.ByteSize = npTermInfo -> bByteSize ;
  110.    dcb.Parity = npTermInfo -> bParity ;
  111.    dcb.StopBits = npTermInfo -> bStopBits ;
  112.  
  113.    // setup hardware flow control
  114.  
  115.    bSet = (BYTE) ((npTermInfo -> bFlowCtrl & FC_HARDWARE) != 0) ;
  116.  
  117.    dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = bSet ;
  118.    dcb.CtsTimeout = (bSet) ? 30 : 0 ;
  119.    dcb.DsrTimeout = (bSet) ? 30 : 0 ;
  120.  
  121.    // setup software flow control
  122.  
  123.    bSet = (BYTE) ((npTermInfo -> bFlowCtrl & FC_SOFTWARE) != 0) ;
  124.  
  125.    dcb.fInX = dcb.fOutX = bSet ;
  126.    dcb.XonChar = ASCII_XON ;
  127.    dcb.XoffChar = ASCII_XOFF ;
  128.    dcb.XonLim = 10 ;
  129.    dcb.XoffLim = 10 ;
  130.  
  131.    // other various settings
  132.  
  133.    dcb.fBinary = TRUE ;
  134.    dcb.fParity = TRUE ;
  135.  
  136.    fRetVal = !(SetCommState( &dcb ) < 0) ;
  137.  
  138.    LocalUnlock( hTermInfo ) ;
  139.    return ( fRetVal ) ;
  140.  
  141. } /* end of SetupConnection() */
  142.  
  143. /************************************************************************
  144.  *  BOOL CloseConnection( HWND hWnd )
  145.  *
  146.  *  Description:
  147.  *     Closes the connection to the port.  Resets the connect flag
  148.  *     in the TERMINFO struct.
  149.  *
  150.  *  Comments:
  151.  *     5/ 9/91  baw  Trying to find something new to comment.
  152.  *
  153.  ************************************************************************/
  154.  
  155. BOOL CloseConnection( HWND hWnd )
  156. {
  157.    LOCALHANDLE  hTermInfo ;
  158.    NPTERMINFO   npTermInfo ;
  159.  
  160.    hTermInfo = GetWindowWord( hWnd, GWW_TERMINFO ) ;
  161.    if (NULL == (npTermInfo = (NPTERMINFO) LocalLock( hTermInfo )))
  162.       return ( FALSE ) ;
  163.  
  164.    KillEvent( hWnd, npTermInfo -> nCid, EV_RXCHAR ) ;
  165.    KillTerminalFocus( hWnd ) ;
  166.    CloseComm( npTermInfo -> nCid ) ;
  167.    npTermInfo -> fConnected = FALSE ;
  168.  
  169.    LocalUnlock( hTermInfo ) ;
  170.    return ( TRUE ) ;
  171.  
  172. } /* end of CloseConnection() */
  173.  
  174. /************************************************************************
  175.  * End of File: connect.c
  176.  ************************************************************************/
  177.  
  178.